/**
 * 
 */
package com.postek.coyote.postekwifi;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.postek.cdf.CDFPTKAndroid;

/**
 * @author yrwang
 *
 */
public class RFIDWriteActivity extends AppCompatActivity {
	
	private CDFPTKAndroid cdf = MainActivity.cdf;
	
	private Button btnPrint = null;
	private Button btnCancel = null;
	
	private Spinner sp_RF1 = null;
	private EditText et_RF2 = null;
	private EditText et_RF3 = null;
	private Spinner sp_RF4 = null;
	private EditText et_RFS = null;
	private EditText et_print_number=null;
	private EditText et_print_cpnumber=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rfid_write);
		
		btnPrint = (Button) findViewById(R.id.btn_printRF_print);
		btnCancel = (Button) findViewById(R.id.btn_printRF_cancel);
		
		sp_RF1 = (Spinner) findViewById(R.id.sp_rfid_RF1);
		et_RF2 = (EditText) findViewById(R.id.et_rfid_RF2);
		et_RF3 = (EditText) findViewById(R.id.et_rfid_RF3);
		sp_RF4 = (Spinner) findViewById(R.id.sp_rfid_RF4);
		et_RFS = (EditText) findViewById(R.id.et_rfid_RFS);
		et_print_number= (EditText) findViewById(R.id.et_print_number);
		et_print_cpnumber= (EditText) findViewById(R.id.et_print_cpnumber);
		
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		btnPrint.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int iRF1 = sp_RF1.getSelectedItemPosition();
				int iRF2 = 0;
				int iRF3 = 0;
				int iRF4 = sp_RF4.getSelectedItemPosition();
				int number=Integer.parseInt(et_print_number.getText().toString());
				int cpnumber=Integer.parseInt(et_print_cpnumber.getText().toString());

				String iRFS = et_RFS.getText().toString();
				
				if (!("".equals(et_RF2.getText().toString()))) {
					iRF2 = Integer.parseInt(et_RF2.getText().toString());
				}
				
				if (!("".equals(et_RF3.getText().toString()))) {
					iRF3 = Integer.parseInt(et_RF3.getText().toString());
				}
				
				int nReturn = cdf.PTK_RWRFIDLabel(1, iRF1, iRF2, iRF3, iRF4, iRFS);
				
				if (nReturn == 0) {
					Toast.makeText(RFIDWriteActivity.this,  R.string.setting_success,
							Toast.LENGTH_SHORT).show();
					cdf.PTK_PrintLabel(number,cpnumber);
				} else {
					Toast.makeText(RFIDWriteActivity.this,  R.string.setting_fail,
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
}
